//
//  SKYEventAttemperEngine.m
//  SKYStockWarning
//
//  Created by Walker on 2016/10/21.
//  Copyright © 2016年 Touker. All rights reserved.
//

#import "SKYEventAttemperEngine.h"

@interface SKYEventAttemperAction : NSObject
@property (nonatomic,copy) void(^callback)(void);
@property (nonatomic,assign) int timer;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) BOOL isValid;
@end

@implementation SKYEventAttemperAction


@end

@interface SKYEventAttemperEngine()

@end

@implementation SKYEventAttemperEngine
{
    NSTimer *_timer;
    NSMutableArray *_actionArray;
}

+ (SKYEventAttemperEngine *)sharedInstance
{
    static dispatch_once_t once;
    static SKYEventAttemperEngine * eventAttemperEngine;
    dispatch_once(&once, ^{
        eventAttemperEngine = [[SKYEventAttemperEngine alloc] init];
    });
    return eventAttemperEngine;
}

- (instancetype)init {
    if (self = [super init]) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        _actionArray = [[NSMutableArray alloc] init];
    }
    return self;
}

static int sky_count = 0;
- (void)timerAction {
    sky_count++;
    for (SKYEventAttemperAction *action in _actionArray) {
        if (action.isValid && sky_count%action.timer == 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                action.callback();
            });
        }
    }
}

- (void)registerAction:(void(^)(void))block timer:(int)timer name:(NSString *)name isValid:(BOOL)isVaild {
    if (timer == 0) {
        return;
    }
    __block BOOL isExist = NO;
    [_actionArray enumerateObjectsUsingBlock:^(SKYEventAttemperAction *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.name isEqualToString:name]) {
            obj.callback = block;
            obj.timer = timer;
            obj.isValid = isVaild;
            isExist = YES;
            *stop = YES;
        }
    }];
    if (isExist) {
        return;
    }
    SKYEventAttemperAction *action = [[SKYEventAttemperAction alloc] init];
    action.callback = block;
    action.timer = timer;
    action.name = name;
    action.isValid = isVaild;
    [_actionArray addObject:action];
}

- (void)registerAction:(void(^)(void))block timer:(int)timer name:(NSString *)name {
    [self registerAction:block timer:timer name:name isValid:YES];
}

- (void)setTimer:(int)timer ForName:(NSString *)name {
    [_actionArray enumerateObjectsUsingBlock:^(SKYEventAttemperAction  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.name isEqualToString:name]) {
            obj.timer = timer;
            *stop = YES;
        }
    }];
}

-(void)setValid:(BOOL)isvalid ForName:(NSString *)name {
    for (SKYEventAttemperAction *action in _actionArray) {
        if ([action.name isEqualToString:name]) {
            action.isValid = isvalid;
            break;
        }
    }
}

- (void)removeEventWithName:(NSString *)name {
    [self suspendEventAttemper];
    for (SKYEventAttemperAction *action in _actionArray) {
        if ([action.name isEqualToString:name]) {
            action.isValid = NO;
            [_actionArray removeObject:action];
            break;
        }
    }
    [self restartEventAttemper];
}

- (void)removeAllEvent {
    [_actionArray removeAllObjects];
}

- (void)suspendEventAttemper {
    [_timer setFireDate:[NSDate distantFuture]];
}

-(void)restartEventAttemper {
    [_timer setFireDate:[NSDate date]];
}


@end

